package com.yilong.springcloudalibabanacos.sys.entity;

import com.yilong.springcloudalibabanacos.sys.enums.ResponseEnum;
import lombok.Data;

import java.io.Serializable;

/**
 * @author SOS
 */

@Data
public class ResponseVO<T> implements Serializable {

    private static final long serialVersionUID = -3632187799188090750L;

    /**
     * 状态码
     */
    private Integer code;

    /**
     * 返回信息
     */
    private String msg;

    /**
     * 返回数据
     */
    private T data;


    /**
     * 条目数
     */
    private Long total;

    /**
     * 返回成功的结果集
     *
     * @return ResponseVO
     */
    public static <T> ResponseVO<T> ok() {
        return ResponseVO.setResult(ResponseEnum.SUCCESS);
    }

    public static <T> ResponseVO<T> ok(T t) {
        return ResponseVO.setResult(ResponseEnum.SUCCESS, t);
    }

    /**
     * 返回成功的结果集
     *
     * @return ResponseVO<T>
     */
    public static <T> ResponseVO<T> ok(T t, long total) {
        ResponseVO<T> r = ResponseVO.setResult(ResponseEnum.SUCCESS, t);
        r.setTotal(total);
        return r;
    }

    /**
     * 返回失败的结果集
     *
     * @return ResponseVO<T>
     */
    public static <T> ResponseVO<T> error() {
        return ResponseVO.setResult(ResponseEnum.ERROR);
    }

    /**
     * 返回失败的结果集
     *
     * @return ResponseVO<T>
     */
    public static <T> ResponseVO<T> error(ResponseEnum responseEnum) {
        return ResponseVO.setResult(responseEnum);
    }

    public static <T> ResponseVO<T> error(String msg) {
        ResponseVO<T> error = ResponseVO.error();
        error.setMsg(msg);
        return error;
    }

    /**
     * 返回失败的结果集
     *
     * @return ResponseVO<T>
     */
    public static <T> ResponseVO<T> setResult(ResponseEnum responseEnum) {
        ResponseVO<T> r = new ResponseVO<>();
        r.setCode(responseEnum.getCode());
        r.setMsg(responseEnum.getMsg());
        return r;
    }

    public static <T> ResponseVO<T> setResult(ResponseEnum responseEnum, T t) {
        ResponseVO<T> r = new ResponseVO<>();
        r.setCode(responseEnum.getCode());
        r.setMsg(responseEnum.getMsg());
        r.setData(t);
        return r;
    }

}
